/*
 * Copyright 2006 Berry Crawford
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.judo.database;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;
import org.judo.database.exceptions.DatabaseConnectionException;
import org.judo.database.orm.mappers.ReflectionMapper;
import org.judo.database.orm.mappers.SimpleJDBCConnection;
import org.judo.database.orm.mappers.SimpleJDBCConnectionFactory;
import org.judo.service.ServiceManager;
import org.judo.util.Util;

public class JudoDatabaseManager
{
	public static HashMap definedDatabases = new HashMap(); 
	
	public static Database getDatabase(String name)
	{
		return (Database)definedDatabases.get(name);
	}
	
	public static JudoDBConnection getConnection(String name) 
		throws DatabaseConnectionException
	{
		Database db = (Database)definedDatabases.get(name);
		return db.getConnectionFactory().getConnection();
	}

	public static void load()
	{
		InputStream is = null;
		
		try
		{
			is =(new JudoDatabaseManager().getClass()).getResourceAsStream(
					"/databases.xml");
			
			SAXBuilder builder = new SAXBuilder();
			Document doc = builder.build(is);
			Element root = doc.getRootElement();
			
			List databases = root.getChildren();
			
			for (int i=0;i<databases.size();i++)
			{
				Element database = (Element)databases.get(i);
			
				Database judoDatabase = null;
				
				String name = database.getAttributeValue("name");
				String type = database.getAttributeValue("type");
				
				if (name == null)
					throw new Exception("There is a database entry in the 'databases.xml' file that does not specify a name.");
				
				if (type == null)
					throw new Exception("There is a database entry in the 'databases.xml' file that does not specify a type.");
				
				if (type.equals("ReflectionMapper"))
					judoDatabase = new ReflectionMapper();
				else
					throw new Exception("The database type '"+type+"' that was specified in the 'databases.xml' file is not a known database type.");
					
				if (judoDatabase == null)
					throw new Exception("Could not find database type '"+type+"'. This is likely an internal Judo bug.  Please report.");
	
				Element connectConfig = database.getChild("connect-method");
				String connectType = connectConfig.getAttributeValue("type");
				
				ConnectionFactory factory = null;
				
				if (connectType.equals("SimpleJDBC"))
				{
					factory = (ConnectionFactory) new SimpleJDBCConnectionFactory();
					factory.init(connectConfig);
				}
				else
					throw new Exception("The connection type '"+type+"' that was specified in the 'databases.xml' file is not a known connection type.");
				
				judoDatabase.defineConnectionFactory(factory);
				definedDatabases.put(name,judoDatabase);
			}
		}
		catch(Throwable e)
		{
			System.out.println("[FATAL ERROR]: could not load Simple URL mappings because: "+e);
			e.printStackTrace();
		}
		finally
		{
			if (is != null)
			{
				try
				{
					is.close();
				} catch (IOException e)
				{
					e.printStackTrace();
				}
			}
		}
	}
}
